package category

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"sola/common"
	"sola/db"
	"sola/router/middleware"
	"sola/utils"
)

func create(c *gin.Context) {
	var category db.Category
	if err := c.ShouldBind(&category); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	if len(category.ClassifyId) <= 0 {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	user, err := middleware.GetUser(c)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeUserNotExist)
		return
	}
	category.AuthorId = user.UserId
	category.AuthorName = user.Username
	category.AuthorNickName = user.Nickname
	category.UpdateUserId = user.UserId
	category.UpdateUserName = user.Username
	category.UpdateUserNickName = user.Nickname
	classify, err := db.FindByClassifyId(category.ClassifyId)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	category.ClassifyName = classify.ClassifyName
	db.Creaete(&category)
	utils.ResponseSuccess(c, &category)
	return
}

func update(c *gin.Context) {
	var category db.Category
	if err := c.ShouldBind(&category); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	var categoryUriInfo db.Category
	if err := c.ShouldBindUri(&categoryUriInfo); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	if category.CategoryId != "" && category.CategoryId != categoryUriInfo.CategoryId {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	user, err := middleware.GetUser(c)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeUserNotExist)
		return
	}
	category.UpdateUserId = user.UserId
	category.UpdateUserName = user.Username
	category.UpdateUserNickName = user.Nickname
	if len(category.ClassifyId) > 0 {
		classify, err := db.FindByClassifyId(category.ClassifyId)
		if err != nil {
			utils.ResponseError(c, utils.ErrCodeParameter)
			return
		}
		category.ClassifyName = classify.ClassifyName
	}
	db.Update(&category)
	utils.ResponseSuccess(c, &category)
	return
}

func list(c *gin.Context) {
	var baseSearchModel common.BaseSearchModel
	if err := c.ShouldBind(&baseSearchModel); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	if err := c.ShouldBindUri(&baseSearchModel); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	if baseSearchModel.UserId == "" {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	data := make(map[string]interface{})
	count := db.CountCategory(&baseSearchModel)
	categories := db.GetCategoryList(&baseSearchModel)
	data["total"] = count
	data["items"] = categories
	utils.ResponseSuccess(c, data)
	return
}

func detail(c *gin.Context) {
	var categoryInfo db.Category
	if err := c.ShouldBindUri(&categoryInfo); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	category, err := db.FindByArticleId(categoryInfo.CategoryId)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	utils.ResponseSuccess(c, category)
	return
}

func delete(c *gin.Context) {
	var category db.Category
	if err := c.ShouldBindUri(&category); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	user, err := middleware.GetUser(c)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeUserNotExist)
		return
	}
	category.UpdateUserId = user.UserId
	category.UpdateUserName = user.Username
	category.UpdateUserNickName = user.Nickname
	db.Delete(&category)
	utils.ResponseSuccess(c, &category)
	return
}
